import {takeEvery, select, call, put, take} from 'redux-saga/effects'
import axios from 'axios'

// 引入状态和常量
import { 
    SENDTASKSAGA, 
    USERINFOCHANGEWORKERTASKDATASAGA, 
    USERINFOCHANGEMASTERTASKDATASAGA,
    UPDATEUSERINFOSAGA,
    UPDATEUSERPASSWDSAGA 
} from '../constants'
import { API_URL } from '../../env'
import { 
    SendTaskSaga, 
    GetUserBaseInfo, 
    ChangeUserinfoActivate, 
    UserinfoChangeWorkerTaskDataActivate,
    UserinfoChangeMasterTaskDataActivate
} from '../actions/userinfo'
import { ChangeSpinACtivate, ChangeBaseUserInfo } from '../actions/public'
import { message } from 'antd'

// 向后端发送，任务信息
function* sendTaskSaga(action) {
    console.log("进入任务发布请求",action.data)
    // 加载状态
    yield put(ChangeSpinACtivate({sendTask_activate: true}))
    const res = yield call(axios.post, API_URL + "task/send", {...action.data})
    if (res.status == 200){
        yield put(GetUserBaseInfo({"money": res.data.money}))
        yield put(ChangeUserinfoActivate({sendTaskActivate:false, msg: res.data.msg}))
        yield put(ChangeSpinACtivate({sendTask_activate: false})) // 请求成功，加载状态清除
        message.success("任务发布成功, 请等待审核")
    } else {
        console.log("userinfosaga 请求失败 ", res)
        yield put(ChangeUserinfoActivate({msg: res.data}))
        yield put(ChangeSpinACtivate({sendTask_activate: false})) // 请求失败，加载状态清除
        message.error("任务发布失败, 请检查网络")
    }
    
}

// 后端获取猎人用户的任务统计面板
function* userInfoChangeWorkerTaskDataSaga(action) {
    try {
        const res = yield call(axios.post, API_URL + "task/worker_task_data_tem", {...action.data})
        if (res.status == 200) {
            yield put(UserinfoChangeWorkerTaskDataActivate(res.data))
        } else {
            message.error("请求失败")
        }
    } catch (e) {
        message.error("请求失败")
    }
}

// 后端获取雇主用户的任务统计面板
function* userInfoChangeMasterTaskDataSaga(action) {
    try {
        const res = yield call(axios.post, API_URL + "task/master_task_data_tem", {...action.data})
        if (res.status == 200) {
            yield put(UserinfoChangeMasterTaskDataActivate(res.data))
        } else {
            message.error("请求失败")
        }
    } catch (e) {
        message.error("请求失败")
    }
}

// 用户修改基本信息
function* updateUserInfoSaga(action) {
    yield put(ChangeSpinACtivate({sendTask_activate: true}))
    try{
        const res = yield call(axios.post, API_URL + "task/update_user_info", {...action.data})
        if (res.data.status == 200) {
            message.success("修改成功")
            yield put(ChangeSpinACtivate({sendTask_activate: false})) // 请求成功，加载状态清除
            yield put(ChangeBaseUserInfo({...res.data.obj}))
            yield put(ChangeUserinfoActivate({
                activate: true,
                updateUserInfoAct: false
            }))
        } else {
            message.error("修改失败")
            yield put(ChangeSpinACtivate({sendTask_activate: false})) // 请求成功，加载状态清除
        }
    } catch (e) {
        message.error("请求失败")
        yield put(ChangeSpinACtivate({sendTask_activate: false})) // 请求成功，加载状态清除
    }
}

// 用户修改密码
function* updateUserPasswdSaga(action) {
    yield put(ChangeSpinACtivate({sendTask_activate: true}))
    try{
        console.log(22222, action.data)
        const res = yield call(axios.post, API_URL + "task/update_user_passwd", {...action.data})
        if (res.data.status == 200) {
            yield put(ChangeSpinACtivate({sendTask_activate: false}))
            yield put(ChangeUserinfoActivate({
                activate: true,
                updateUserInfoAct: false
            }))
            message.success("密码修改成功")
        } else if (res.data.status == 4000) {
            message.warning("输入的老密码不正确，请重新输入")
            yield put(ChangeSpinACtivate({sendTask_activate: false}))
        } else if (res.data.status == 4001) {
            message.warning("新密码不能与老密码相同，请重新输入")
            yield put(ChangeSpinACtivate({sendTask_activate: false}))
        } else {
            message.error("请求失败")
            yield put(ChangeSpinACtivate({sendTask_activate: false}))
        }
        
    } catch (e) {
        message.error("请求失败")
        yield put(ChangeSpinACtivate({sendTask_activate: false}))
    }
}

export function* userinfoSaga () {
    yield takeEvery(SENDTASKSAGA, sendTaskSaga)
    yield takeEvery(USERINFOCHANGEWORKERTASKDATASAGA, userInfoChangeWorkerTaskDataSaga)
    yield takeEvery(USERINFOCHANGEMASTERTASKDATASAGA, userInfoChangeMasterTaskDataSaga)
    yield takeEvery(UPDATEUSERINFOSAGA, updateUserInfoSaga)
    yield takeEvery(UPDATEUSERPASSWDSAGA, updateUserPasswdSaga)
}
